package com.tengju.bff.interfaces.shared;

import com.tengju.bff.interfaces.shared.render.MetaUtil;
import com.tengju.data.domain.model.analysis.Meta;
import com.tengju.data.domain.model.analysis.MetaType;
import com.tengju.data.domain.model.analysis.Row;
import com.tengju.data.domain.model.analysis.SensitiveLevel;
import com.tengju.data.domain.shared.CastUtil;
import com.tengju.user.domain.shared.SensitiveFieldType;
import org.springframework.util.CollectionUtils;

import javax.validation.constraints.NotNull;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author chenhang
 * @Date 2021/5/6
 */
public class TableAssembler {

    private TableAssembler() {
    }

    public static TableVO buildColumns(@NotNull TableVO table, @NotNull Collection<Meta> metaList) {
        if (CollectionUtils.isEmpty(metaList)) {
            return table;
        }
        Map<String,ColumnVO> columnGroupMap = new HashMap<>();
        for (Meta meta : metaList) {
            if (!meta.getIsShow()) {
                continue;
            }
            ColumnVO columnVO = ColumnVO.of(meta.getColName(), meta.getShowName(), meta.getFrontPageSort());
            columnVO.setMeta(meta);
            if (Objects.isNull(meta.getColumnGroup())) {
                table.addColumn(columnVO);
                continue;
            }
            ColumnVO groupColumnVO = columnGroupMap.get(meta.getColumnGroup());
            if (groupColumnVO == null) {
                groupColumnVO = ColumnVO.of(meta.getColumnGroup(), meta.getColumnGroup(),false);
                columnGroupMap.put(meta.getColumnGroup(),groupColumnVO);
                table.addColumn(groupColumnVO);
            }
            groupColumnVO.addChild(columnVO);
        }
        return table;
    }

    /**
     * meta作为列
     * @param table
     * @param rows
     * @param metaList
     * @param externalKeys
     * @return
     */
    public static void buildData(@NotNull TableVO table,
                                    @NotNull Collection<Row> rows,
                                    Collection<Meta> metaList,
                                    Collection<String> externalKeys) {
        if (CollectionUtils.isEmpty(rows)) {
            return;
        }
        List<Map<String, Object>> records = new ArrayList<>();
        for (Row row : rows) {
            Map<String, Object> record = new HashMap<>();
            if (!CollectionUtils.isEmpty(metaList)) {
                for (Meta meta : metaList) {
                    if (!meta.getIsShow()) {
                        continue;
                    }
                    AmountVO amount = MetaUtil.getAmount(meta,row,metaList);
                    record.put(meta.getColName(),amount);
                }
            }
            if (!CollectionUtils.isEmpty(externalKeys)) {
                for (String externalKey : externalKeys) {
                    record.put(externalKey,row.getValue(externalKey));
                }
            }
            records.add(record);
        }
        table.setRecords(records);
    }




    /**
     * meta作为列
     * @param table
     * @param rows
     * @param metaList
     * @return
     */
    public static void buildData(@NotNull TableVO table,
                                    @NotNull Collection<Row> rows,
                                    Collection<Meta> metaList) {
        buildData(table,rows,metaList,null);
    }





    /**
     * meta作为行
     * 第一列固定取meta#showName
     * @param table
     * @param rowMap
     * @param metas
     */
    public static void buildVerticalData(TableVO table, Map<String, Row> rowMap, Collection<Meta> metas) {
        if (CollectionUtils.isEmpty(rowMap)) {
            return;
        }
        List<ColumnVO> headers = table.getHeader();
        List<Map<String, Object>> records = new ArrayList<>();
        for (Meta meta : metas) {
            Map<String, Object> record = new HashMap<>();
            record.put("showName",meta.getShowName());
            for (ColumnVO header : headers) {
                Row row = rowMap.get(CastUtil.castString(header.getProp()));
                if (row == null) {
                    continue;
                }
                record.put(header.getProp(),Formatter.formatter(meta,row.getValue(meta.getSqlColName())));
            }
            records.add(record);
        }
        table.setRecords(records);
    }


}
